<template>
  <div class="room-list">
    <!-- 添加创建房间按钮 -->
    <div class="room-list-header-actions">
      <button @click="showCreateRoomDialog = true" class="create-btn">创建房间</button>
    </div>
    
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <div v-else-if="rooms && rooms.length > 0">
      <div v-for="room in rooms" :key="getRoomId(room)" :class="['room-item', { 'new-room': getRoomId(room) === newRoomId }]">
        <div class="room-info">
          <div class="room-name">{{ room.name }}</div>
          <div class="room-meta">
            <span class="room-users">{{ room.user_count || 0 }}/{{ room.max_users || 2 }} 人</span>
            <span class="room-type">{{ getRoomTypeText(room.tag || room.type) }}</span>
          </div>
        </div>
        <div :class="['room-status', { 'is-playing': room.is_playing }]">{{ room.is_playing ? '游戏中' : '等待中' }}</div>
        <div class="room-actions">
          <button @click="joinRoom(getRoomId(room))" class="join-btn">加入</button>
          <!-- 只有房间创建者可以看到删除按钮 -->
          <button 
            v-if="isRoomCreator(room)" 
            @click="deleteRoom(room)" 
            class="delete-btn"
          >
            删除
          </button>
        </div>
      </div>
    </div>

    <div v-else class="empty-state">
      <div class="empty-state-icon">📭</div>
      <p>暂无可用房间，创建一个新房间开始游戏吧！</p>
      <button @click="showCreateRoomDialog = true" class="create-btn">创建房间</button>
    </div>

    <!-- 创建房间弹窗 -->
    <div v-if="showCreateRoomDialog" class="dialog-overlay">
      <div class="dialog-content">
        <h3>创建房间</h3>
        <input
          v-model="roomName"
          placeholder="输入房间名称"
          class="room-name-input"
          :class="{ 'error': roomNameError }"
        >
        <div v-if="roomNameError" class="error-message">{{ roomNameError }}</div>
        
        <select v-model="roomType" class="room-type-select">
          <option value="chat">聊天</option>
          <option value="snake">蛇蛇大作战</option>
          <option value="tictactoe">井字棋</option>
          <option value="gomoku">五子棋</option>
        </select>
        
        <input
          v-model="roomPassword"
          placeholder="输入房间密码（可选）"
          type="password"
          class="room-password-input"
        >
        
        <textarea
          v-model="roomDescription"
          placeholder="房间描述（可选）"
          class="room-description-textarea"
        ></textarea>
        
        <div class="dialog-actions">
          <button @click="showCreateRoomDialog = false" class="cancel-btn">取消</button>
          <button @click="createRoom" class="confirm-btn">创建</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';

const props = defineProps({
  rooms: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  },
  newRoomId: {
    type: String,
    default: ''
  }
});

const emit = defineEmits(['join-room', 'delete-room', 'create-room']);

const showCreateRoomDialog = ref(false);
const roomName = ref('');
const roomType = ref('chat');
const roomPassword = ref('');
const roomDescription = ref('');
const roomNameError = ref('');

const getRoomId = (room) => {
  return room.id || room._id;
};

const getRoomTypeText = (type) => {
  const types = {
    chat: '聊天',
    snake: '蛇蛇大作战',
    tictactoe: '井字棋',
    gomoku: '五子棋'
  };
  return types[type] || type;
};

const isRoomCreator = (room) => {
  // 实现检查当前用户是否是房间创建者的逻辑
  return false;
};

const joinRoom = (roomId) => {
  emit('join-room', roomId);
};

const deleteRoom = (room) => {
  emit('delete-room', room);
};

const createRoom = () => {
  if (!roomName.value.trim()) {
    roomNameError.value = '请输入房间名称';
    return;
  }
  
  const roomData = {
    name: roomName.value,
    type: roomType.value,
    password: roomPassword.value,
    description: roomDescription.value
  };
  
  emit('create-room', roomData);
  
  // 重置表单
  showCreateRoomDialog.value = false;
  roomName.value = '';
  roomType.value = 'chat';
  roomPassword.value = '';
  roomDescription.value = '';
  roomNameError.value = '';
};
</script>

<style scoped>
.room-list {
  margin-top: 20px;
}

.room-list-header-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 15px;
}

.create-btn {
  padding: 8px 16px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.create-btn:hover {
  background-color: #40a9ff;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 0;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.room-item {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  margin-bottom: 10px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.2s;
}

.room-item:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  transform: translateY(-1px);
}

.room-item.new-room {
  border-left: 4px solid #1890ff;
}

.room-info {
  flex: 1;
}

.room-name {
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 5px;
}

.room-meta {
  display: flex;
  gap: 10px;
  font-size: 12px;
  color: #666;
}

.room-status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  background-color: #f5f5f5;
  color: #666;
  margin: 0 15px;
}

.room-status.is-playing {
  background-color: #fff2f0;
  color: #ff4d4f;
}

.room-actions {
  display: flex;
  gap: 10px;
}

.join-btn {
  padding: 6px 12px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}

.join-btn:hover {
  background-color: #40a9ff;
}

.delete-btn {
  padding: 6px 12px;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}

.delete-btn:hover {
  background-color: #ff7875;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  text-align: center;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.empty-state-icon {
  font-size: 40px;
  margin-bottom: 15px;
  color: #ccc;
}

.empty-state p {
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

/* 弹窗样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-content {
  background-color: white;
  border-radius: 8px;
  width: 90%;
  max-width: 400px;
  padding: 20px;
}

.dialog-content h3 {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}

.room-name-input,
.room-type-select,
.room-password-input,
.room-description-textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 15px;
}

.room-name-input.error {
  border-color: #ff4d4f;
}

.error-message {
  color: #ff4d4f;
  font-size: 12px;
  margin-top: -10px;
  margin-bottom: 15px;
}

.room-description-textarea {
  min-height: 80px;
  resize: vertical;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.cancel-btn {
  padding: 8px 16px;
  background-color: #f5f5f5;
  color: #666;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
}

.cancel-btn:hover {
  background-color: #e0e0e0;
}

.confirm-btn {
  padding: 8px 16px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
}

.confirm-btn:hover {
  background-color: #40a9ff;
}
</style>